package com.sitech.ibnms.core;

import com.sitech.ibnms.util.JSONUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Page implements IPage {

	/**
	 * 总页数
	 */
	private int total;
	/**
	 * 每页行数
	 */
	private int pageSize;
	/**
	 * 当前页数
	 */
	private int page;
	/**
	 * 总记录条数
	 */
	private int records;
	/**
	 * 查询结果
	 */
	private Collection rows;
	/**
	 * 当前rows的父节点id，当表格模式为treeGrid时使用
	 */
	private String parentId;
	/**
	 * 当前数据的层级，当表格模式为treeGrid时使用
	 */
	private int level;
	private String responseText;

	public String getResponseText() {
		return responseText;
	}

	public void setResponseText(String responseText) {
		this.responseText = responseText;
	}

	/**
	 * Creates a new instance of Page.
	 *
	 * @param rows 查询结果
	 * @param records 记录条数
	 * @param pageSize 每页条数
	 * @param page	当前页数
	 */
	public Page(Collection rows, int records, int pageSize, int page) {
		this.rows = ((Collection) (rows != null ? rows
				: ((Collection) (new ArrayList(0)))));
		this.records = records;
		this.page = page;
		this.pageSize = pageSize;
		total = calcTotalPage();
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setRecords(int records) {
		this.records = records;
	}

	public void setRows(Collection rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public int getPageSize() {
		return pageSize;
	}

	private int calcTotalPage() {
		int t = getRecords();
		int p = getPageSize();
		if (t == 0 || p == 0) {
			return 0;
		}
		int r = t % p;
		int pages = (t - r) / p;
		if (r > 0) {
			pages++;
		}
		return pages;
	}

	public int getPage() {
		return page;
	}

	public int getPageBegin() {
		return pageSize * page;
	}

	public int getPageEnd() {
		return getPageBegin() + getRows().size();
	}

	public Collection getRows() {
		return rows;
	}

	public boolean isFirstPage() {
		return page == 0;
	}

	public boolean isLastPage() {
		return page == total;
	}

	public boolean add(Object o) {
		return rows.add(o);
	}

	public boolean addAll(Collection c) {
		return rows.addAll(c);
	}

	public void clear() {
		rows.clear();
	}

	public boolean contains(Object o) {
		return rows.contains(o);
	}

	public boolean containsAll(Collection c) {
		return rows.containsAll(c);
	}

	@Override
	public boolean equals(Object o) {
		return rows.equals(o);
	}

	@Override
	public int hashCode() {
		return rows.hashCode();
	}

	public boolean isEmpty() {
		return rows.isEmpty();
	}

	public Iterator iterator() {
		return rows.iterator();
	}

	public boolean remove(Object o) {
		return rows.remove(o);
	}

	public boolean removeAll(Collection c) {
		return rows.removeAll(c);
	}

	public boolean retainAll(Collection c) {
		return rows.retainAll(c);
	}

	public int size() {
		return rows.size();
	}

	public Object[] toArray() {
		return rows.toArray();
	}

	public Object[] toArray(Object a[]) {
		return rows.toArray(a);
	}

	public int getRecords() {
		return records;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	@Override
	public String toString() {
		return JSONUtil.toJSON(this);
	}
}
